package com.jielihaofeng.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jielihaofeng.chat.service.ChatUserRelationService;
import com.jielihaofeng.chat.vo.ChatUserRelationVO;
import lombok.AllArgsConstructor;
import com.jielihaofeng.chat.convert.ChatUserRelationConvert;
import com.jielihaofeng.chat.dao.ChatUserRelationDao;
import com.jielihaofeng.chat.entity.ChatUserRelationEntity;
import com.jielihaofeng.chat.query.ChatUserRelationQuery;
import com.jielihaofeng.framework.common.page.PageResult;
import com.jielihaofeng.framework.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户好友表
 *
 * @author 借力好风 949394048@qq.com
 * @since 1.0.0 2022-07-22
 */
@Service
@AllArgsConstructor
public class ChatUserRelationServiceImpl extends BaseServiceImpl<ChatUserRelationDao, ChatUserRelationEntity> implements ChatUserRelationService {

    @Override
    public PageResult<ChatUserRelationVO> page(ChatUserRelationQuery query) {
        IPage<ChatUserRelationEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(ChatUserRelationConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private QueryWrapper<ChatUserRelationEntity> getWrapper(ChatUserRelationQuery query) {
        QueryWrapper<ChatUserRelationEntity> wrapper = new QueryWrapper<>();

        return wrapper;
    }

    @Override
    public boolean save(ChatUserRelationVO vo) {
        ChatUserRelationEntity entity = ChatUserRelationConvert.INSTANCE.convert(vo);

        return baseMapper.insert(entity) >0;
    }

    @Override
    public void update(ChatUserRelationVO vo) {
        ChatUserRelationEntity entity = ChatUserRelationConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    /**
     * 获取好友id列表
     *
     * @param userId 用户id
     * @return 好友id列表
     */
    @Override
    public List<Long> getFriendIdListByStatus(Long userId, Integer status) {
        return baseMapper.getFriendIdListByStatus(userId, status);
    }

    @Override
    public List<Long> getRequestFriendIdList(Long userId) {
        return baseMapper.getRequestFriendIdList(userId);
    }

    @Override
    public List<ChatUserRelationEntity> getAllFriend(Long userId) {
        return baseMapper.getAllFriend(userId);
    }

    /**
     * 添加好友
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return 添加结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addFriend(Long userId, Long friendId) {
        List<Long> friendIdList = baseMapper.getFriendIdListByStatus(userId, null);
        if (friendIdList.contains(friendId)) {
            return false;
        }

//        // 暂时用冗余数据玩法
//        ChatUserRelationEntity chatUserRelationEntity = new ChatUserRelationEntity();
//        chatUserRelationEntity.setUserId(userId);
//        chatUserRelationEntity.setFriendId(friendId);
//        baseMapper.insert(chatUserRelationEntity);
//        ChatUserRelationEntity chatUserRelationEntityReverse = new ChatUserRelationEntity();
//        chatUserRelationEntityReverse.setUserId(friendId);
//        chatUserRelationEntityReverse.setFriendId(userId);
//        baseMapper.insert(chatUserRelationEntity);

        ChatUserRelationEntity chatUserRelationEntityReverse = new ChatUserRelationEntity();
        chatUserRelationEntityReverse.setUserId(friendId);
        chatUserRelationEntityReverse.setFriendId(userId);
        chatUserRelationEntityReverse.setStatus(1);
        baseMapper.insert(chatUserRelationEntityReverse);
        return true;
    }

    /**
     * 删除好友
     *
     * @param userId   用户id
     * @param friendId 好友id
     * @return 删除结果
     */
    @Override
    public boolean deleteFriend(Long userId, Long friendId) {
        baseMapper.delete(new LambdaQueryWrapper<ChatUserRelationEntity>()
                .eq(ChatUserRelationEntity::getUserId, userId)
                .eq(ChatUserRelationEntity::getFriendId, friendId)
                .or(wrapper -> wrapper.eq(ChatUserRelationEntity::getUserId, friendId).eq(ChatUserRelationEntity::getFriendId, userId))
        );
        return true;
    }

    /**
     * 根据用户id和好友id查询记录
     * @param userId 用户id
     * @param friendId 好友id
     * @return 记录
     */
    @Override
    public ChatUserRelationEntity queryByUserIdAndFriendId(Long userId, Long friendId) {
        LambdaQueryWrapper<ChatUserRelationEntity> wrapper = new LambdaQueryWrapper<ChatUserRelationEntity>().eq(ChatUserRelationEntity::getUserId, userId)
                .eq(ChatUserRelationEntity::getFriendId, friendId);
        ChatUserRelationEntity chatUserRelationEntity = baseMapper.selectOne(wrapper);
        return chatUserRelationEntity;
    }
}